package com.study.lession.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * 字符串常用操作工具类
 *
 * @since 1.0 , Java 1.8 , Sep 13, 2016
 * @version 1.0
 * @author Qt
 */
public class StringUtil {

	/** 文件夹分隔符 */
	public static final String FOLDER_SEPARATOR = "/";

	/** 空字符串 */
	public static final String EMPTY = "";

	/** null字符串 */
	public static final String NULL = "null";

	/** 逗号 */
	public static final String COMMA = ",";

	/** 分号分 */
	public static final String SEMICOLON = ";";

	/** 冒号 */
	public static final String COLON = ":";

	/** 小于号 */
	public static final String LT = "<";

	/** 大于号 */
	public static final String GT = ">";

	/** 点号 */
	public static final String DOT = ".";

	/** 下划线 */
	public static final String UNDERLINE = "_";

	/** 分号分隔符(不区分全半角) */
	public static final String SPLIT_SEMICOLON = ";|；";

	/** 逗号分隔符(不区分全半角) */
	public static final String SPLIT_COMMA = ",|，";

	/** 索引位置找不到 */
	public static final int INDEX_NOT_FOUND = -1;

	/** 整数特殊值 */
	public static final int NUM_FLAG = -1;

	/** 长整数特殊值 */
	public static final long LONG_NUM_FLAG = -1;

	/** 换行 */
	public static final String NEW_LINE = "\n";

	/**
	 * String -> unicode
	 * @param s
	 * @return
	 */
	public static String toUnicodeString(String s) {
		StringBuilder sb = new StringBuilder();
		char[] chs = s.toCharArray();
		for (int i = 0; i < chs.length; i++) {
			if (chs[i] > 0 && chs[i] < 127 && chs[i] != '\\') {
				sb.append(chs[i]);
			} else {
				String str = Integer.toString(chs[i], 16);
				sb.append("\\u").append("0000".substring(0, 4 - str.length())).append(str);
			}
		}
		return sb.toString();
	}

	/**
	 * unicode->String
	 * @param s
	 * @return
	 */
	public static String fromUnicodeString(String s) {
		StringBuilder sb = new StringBuilder();
		char[] chs = s.toCharArray();
		for (int i = 0; i < chs.length; i++) {
			if (chs[i] == '\\' && i + 5 < chs.length && Character.toLowerCase(chs[i + 1]) == 'u') {
				try {
					short sht = (short) Integer
							.parseInt(new String(new char[] { chs[i + 2], chs[i + 3], chs[i + 4], chs[i + 5] }), 16);
					sb.append((char) sht);
					i += 5;
				} catch (NumberFormatException e) {
					sb.append(chs[i]);
				}
			} else {
				sb.append(chs[i]);
			}
		}
		return sb.toString();
	}

	/**
	 * 为空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(final String str) {
		return "".equals(StringUtil.trimToEmpty(str));
	}

	/**
	 * 不为空
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(final String str) {
		return null != StringUtil.trimToNull(str);
	}

	/**
	 * 计算16进制对应的数值
	 * @param ch
	 * @return
	 * @throws Exception
	 */
	public static int GetHex(char ch) throws Exception {
		if (ch >= '0' && ch <= '9')
			return (int) (ch - '0');
		if (ch >= 'a' && ch <= 'f')
			return (int) (ch - 'a' + 10);
		if (ch >= 'A' && ch <= 'F')
			return (int) (ch - 'A' + 10);
		throw new Exception("error param");
	}

	/**
	 * 计算幂
	 * @param nValue
	 * @param nCount
	 * @return
	 * @throws Exception
	 */
	public static int GetPower(int nValue, int nCount) throws Exception {
		if (nCount < 0)
			throw new Exception("nCount can't small than 1!");
		if (nCount == 0)
			return 1;
		int nSum = 1;
		for (int i = 0; i < nCount; ++i) {
			nSum = nSum * nValue;
		}
		return nSum;
	}

	/**
	 * trim
	 * @param str
	 * @return
	 */
	public static String trim(final String str) {
		if (str == null) {
			return null;
		}
		return str.trim();
	}

	/**
	 * trimToEmpty
	 * @param o
	 * @return
	 */
	public static String trimToEmpty(final Object o) {
		if (o == null) {
			return "";
		}
		if (o.getClass() == String.class) {
			return ((String) o).trim();
		} else {
			return o.toString().trim();
		}
	}

	/**
	 * trimToNull
	 * @param o
	 * @return
	 */
	public static String trimToNull(final Object o) {
		if (o == null) {
			return null;
		}
		String str;
		if (o.getClass() == String.class) {
			str = ((String) o).trim();
		} else {
			str = o.toString().trim();
		}
		return "".equals(str) ? null : str;
	}

	/**
	 * uuid获取
	 * @return
	 */
	public static String uuid() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replace("-", "");
	}

	/**
	 * 将字符串按逗号分隔，转成字符串集合<br>
	 *
	 *
	 *
	 * @param str
	 * @return 当字符串为空时，返回空集合
	 */
	public static List<String> toStrList(String str) {
		return toStrList(str, COMMA);
	}

	/**
	 * 将字符串按分隔符分隔，转成字符串集合
	 *
	 * @param str
	 * @param split
	 * @return 当字符串为空时，返回空集合
	 */
	public static List<String> toStrList(String str, String split) {
		if (isEmpty(str)) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(Arrays.asList(str.split(split)));
	}

	/**
	 * 将字符串按分隔符分隔，转成字符串trim后的集合
	 *
	 * @param str
	 * @param split
	 * @return
	 */
	public static List<String> toTrimList(String str, String split) {
		return toTrimList(str, split, false);
	}

	/**
	 * 将字符串按分隔符分隔，转成字符串trim后的集合，支持去除空白字符
	 *
	 * @param str
	 * @param split
	 * @param excludeBlank
	 *            是否除排空白字符
	 * @return
	 */
	public static List<String> toTrimList(String str, String split, boolean excludeBlank) {
		if (isEmpty(str)) {
			return new ArrayList<String>();
		}
		List<String> strList = new LinkedList<String>();
		String[] strArr = str.split(split);
		for (int i = 0; i < strArr.length; i++) {
			String tempStr = strArr[i];
			if (excludeBlank) {
				if (isEmpty(tempStr)) {
					continue;
				}
			}
			strList.add(strArr[i].trim());
		}
		return strList;
	}

	/**
	 * 将字符串按逗号分隔，转成Long集合
	 *
	 * @param str
	 * @return 当字符串为空时，返回空集合
	 */
	public static List<Long> toLongList(String str) {
		return toLongList(str, COMMA);
	}

	/**
	 * 将字符串按逗号分隔，转成整数集合
	 *
	 * @param str
	 * @return 当字符串为空时，返回空集合
	 */
	public static List<Integer> toIntList(String str) {
		return toIntList(str, COMMA);
	}

	/**
	 * 将字符串按逗号分隔，转成整数集合（注意：不能转换为整数的会被过滤掉）
	 *
	 * @param str
	 * @param split
	 * @return
	 */
	public static List<Long> toLongList(String str, String split) {
		if (isEmpty(str)) {
			return new ArrayList<Long>();
		}
		List<Long> longList = new LinkedList<Long>();
		String[] strArr = str.split(split);
		for (int i = 0; i < strArr.length; i++) {
			Long num = getLong(strArr[i]);
			if (num != null) {
				longList.add(num);
			}
		}
		return longList;
	}

	/**
	 * 将字符串按逗号分隔，转成整数集合（注意：不能转换为整数的会被过滤掉）
	 *
	 * @param str
	 * @param split
	 * @return
	 */
	public static List<Integer> toIntList(String str, String split) {
		if (isEmpty(str)) {
			return new ArrayList<Integer>();
		}
		List<Integer> intList = new LinkedList<Integer>();
		String[] strArr = str.split(split);
		for (int i = 0; i < strArr.length; i++) {
			Integer num = getInteger(strArr[i]);
			if (num != null) {
				intList.add(num);
			}
		}
		return intList;
	}

	/**
	 * 将字符串转换为整形形式，无法转换则返回null
	 *
	 * @param str
	 * @return
	 */
	public static Integer getInteger(String str) {
		if (isEmpty(str)) {
			return null;
		}
		try {
			return Integer.parseInt(str);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 将字符串转换为整形形式，无法转换则返回null
	 *
	 * @param str
	 * @return
	 */
	public static int getInt(String str) {
		if (isEmpty(str)) {
			return 0;
		}
		try {
			return Integer.parseInt(str);
		} catch (Exception ex) {
			return 0;
		}
	}

	/**
	 * 将字符串转换为Long形式，无法转换则返回null
	 *
	 * @param str
	 * @return
	 */
	public static Long getLong(String str) {
		if (isEmpty(str)) {
			return null;
		}
		try {
			return Long.parseLong(str);
		} catch (Exception ex) {
			return null;
		}
	}
	
	/**
	 * 将字符串转换为Long形式，无法转换则返回null
	 *
	 * @param str
	 * @return
	 */
	public static Long getLong(String str, Long defaultValue) {
		Long value = getLong(str);
		if (null != value) {
			return value;
		} else {
			return defaultValue;
		}
	}

}
